let app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        spinShow: false,
        loadMoreLoad: false,
        loadMoreTip: '',
        queryParam: {
            pageNum: 1,
            pageSize: app.globalData.pageSize,
            customerId: null
        },
        tableData: [],
        pages: 0,
        calendarShow: false,
        rangeData: ''
    },

    onPullDownRefresh() {
        this.setData({
            spinShow: true
        });
        if (!this.loading) {
            this.setData({
                ['queryParam.pageNum']: 1,
                loadMoreLoad: false,
                loadMoreTip: '正在刷新',
            });
            this.queryData(true)
        }
    },
    onReachBottom() {
        if (!this.loading && this.data.queryParam.pageNum < this.data.pages) {
            this.setData({
                loadMoreLoad: true,
                loadMoreTip: '正在加载'
            });
            this.setData({
                ['queryParam.pageNum']: this.data.queryParam.pageNum + 1
            });
            this.queryData(false)
        }
    },
    queryData: function (override) {
        let _this = this
        app.formGet('/app/customer/bill/page', this.data.queryParam).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                let tableData = override ? res.data.list : this.data.tableData.concat(res.data.list)
                _this.setData({
                    tableData: tableData,
                    pages: res.data.pageInfo.pages
                });
                if (this.data.queryParam.pageNum >= res.data.pageInfo.pages) {
                    this.setData({
                        loadMoreLoad: false,
                        loadMoreTip: '暂无更多数据'
                    });
                }
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })

        app.formGet('/app/customer/bill/stat', null).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                _this.setData({
                    unSettleAmount: res.data.unSettleAmount
                });
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log("options")
        console.log(options)
        let customerId = options.customerId
        this.setData({
            spinShow: true,
            queryParam: {
                pageNum: 1,
                pageSize: app.globalData.pageSize,
                customerId: customerId
            },
            loadMoreLoad: false,
            loadMoreTip: ''
        });
    },
    onBillClick: function (event) {
        let index = event.currentTarget.id
        let tableData = this.data.tableData
        let item = tableData[index]
        let type = item.type
        if (type == 3) {
            wx.navigateTo({
                url: "/pages/customer/settle/detail/settleDetail?customerBillId=" + item.id
            })
        } else if (type == 2) {
            wx.navigateTo({
                url: "/print-package/pages/orderback/detail/orderBackDetail?orderId=" + item.businessId
            })
        } else {
            wx.navigateTo({
                url: "/print-package/pages/order/prepare/index?orderId=" + item.businessId
            })
        }
    },
    onShow: function (options) {
        this.queryData(true)
    },
    onClose() {
        this.setData({calendarShow: false});
    },
    formatDate(date) {
        date = new Date(date);
        return `${date.getMonth() + 1}/${date.getDate()}`;
    },
    onConfirm(event) {
        const [start, end] = event.detail;
        this.setData({
            calendarShow: false,
            rangeData: `${this.formatDate(start)} - ${this.formatDate(end)}`,
        });
    },


})
